//
//  EditCourseViewController.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/30.
//  Copyright © 2016年 Gcx. All rights reserved.
//
#define REDCOLOR [self getColor:@"e86666"]

#import "EditCourseViewController.h"
#import "ClassManagerModel.h"
#import "SC_TextFeild.h"
#import "SCTypeBtn.h"
#import "UILabel+SCAdd.h"
#import "MMLabel.h"

@interface EditCourseViewController(){

    ClassManagerModel   *_myModel;
    SC_TextFeild        *_classNameTF;
    SC_TextFeild        *_classTimeTF;
    SCTypeBtn           *_succeedBtn;
    
}

@property (nonatomic,strong) NSArray *fieldArr;


@end

@implementation EditCourseViewController

- (void)viewDidLoad{
    
    [super viewDidLoad];

    self.view.backgroundColor = BGC;
    
    [self.navbar setTitle:@"编辑课程"];
    
    [self addRightBtn];
    
    [self creatMainView];
}

- (void)addRightBtn{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 40, 20);
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = NormalFont(42);
    [btn addTarget: self action:@selector(loginBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.navbar setRightBtn:btn];
}


- (void)loginBtnClicked:(UIButton *)sender{
    
    NSArray *array = [_classNameTF.text componentsSeparatedByString:@" "];
    
    if (_classNameTF.text.length == 0 || array.count == _classNameTF.text.length + 1) {
        
        [[JLActionManager sharedInstance]showHUDWithLabelText:@"课程名称不能为空" imageView:FailImage inView:self.view];
        
    } else if (_classNameTF.text.length > 50){
        
        [[JLActionManager sharedInstance]showHUDWithLabelText:@"课程名称不能超过50位" imageView:FailImage inView:self.view];
        
    }else {
        
        [self creatClass];
    }
}

#pragma mark -- 请求数据

- (void)creatClass{

    NSDictionary *dic = @{@"oauth_token":@"815bffb68e2ea44e38cf16a98a5a29a4",@"oauth_token_secret":@"8e84bc64f03d73b5c75406ef1d7349f7"};
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];
    
    [params setObject:_myModel.kcid forKey:@"kcid"];
    [params setObject:_classNameTF.text forKey:@"kcmc"];
    
    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_updateCourse params:params];
    
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        if ([resp.originalDictionary[@"status"]integerValue] == 0) {
        
            POST_SUCCEED(resp.originalDictionary[@"statusCode"]);

            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                self.myBlock();
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else {
        
            POST_FAIL(resp.originalDictionary[@"statusCode"]);
        }
        
    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
    }];
    
}

- (void)creatMainView{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SIZEWIDTH, ScaleSize(336))];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIView *xiahuaxianView = [[UIView alloc]initWithFrame:CGRectMake(ScaleSize(42), ScaleSize(168), SIZEWIDTH - ScaleSize(42)*2, 0.5)];
    xiahuaxianView.backgroundColor = colordbdbdb;
    [view addSubview:xiahuaxianView];

    MMLabel *nameLabel = [[MMLabel alloc]initWithFrame:CGRectMake(ScaleSize(32), ScaleSize(63), 75, ScaleSize(42)) ];
    
    
    nameLabel.text =@"* 课程名称:";
    nameLabel.font = NormalFont(42);
    nameLabel.textColor = REDCOLOR;
    nameLabel.keyWord = @"课程名称:";
    nameLabel.keyWordColor =Color333333;
    
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScaleSize(42), CGRectGetMaxY(xiahuaxianView.frame)+ScaleSize(63), 75, ScaleSize(42))];
    timeLabel.text = @" 上课学期:";
    timeLabel.font = NormalFont(42);
    timeLabel.textColor = Color333333;

    [view addSubview:nameLabel];
    [view addSubview:timeLabel];

    
    _classNameTF = [[SC_TextFeild alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame)+7, ScaleSize(63), SIZEWIDTH - 90, ScaleSize(42))];
    _classTimeTF = [[SC_TextFeild alloc]initWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame)+5, CGRectGetMaxY(xiahuaxianView.frame)+ScaleSize(63), SIZEWIDTH - 90, ScaleSize(42))];
    
    [_classNameTF setValue:Color333333 forKeyPath:@"_placeholderLabel.textColor"];
    [_classTimeTF setValue:Color333333 forKeyPath:@"_placeholderLabel.textColor"];
    
    _classTimeTF.userInteractionEnabled = NO;
    
    _classTimeTF.text = _myModel.skxq;
    _classNameTF.text = _myModel.kcmc;
    
    [view addSubview:_classTimeTF];
    [view addSubview:_classNameTF];
    
}

-(UIColor *)getColor:(NSString *)hexColor {
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

- (id)initDataClassManagerModel:(ClassManagerModel *)model{

    _myModel = model;
    
    return self;
}

- (NSArray *)fieldArr{

    if (!_fieldArr) {
        
        _fieldArr = [NSArray array];
    }
    return _fieldArr;
}

@end
